package com.sunlg.smarthome.ui.curtain;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.CurtainContract;
import com.sunlg.smarthome.contract.presenter.CurtainPresenter;
import com.sunlg.smarthome.data.entity.Curtain;
import com.sunlg.smarthome.ui.wedget.CurtainView;

import butterknife.BindView;

public class CurtainActivity extends BaseActivity
        implements CurtainContract.CurtainView, View.OnClickListener{

    @BindView(R.id.curtain_view)
    CurtainView mCurtainView;
    @BindView(R.id.seekBar_status)
    SeekBar mSeekBarStatus;
    @BindView(R.id.seekBar_speed)
    SeekBar mSeekBarSpeed;
    @BindView(R.id.rg_curtain)
    RadioGroup radioGroup;
    @BindView(R.id.rb_unfold)
    RadioButton rbUnfold;
    @BindView(R.id.rb_stop)
    RadioButton rbStop;
    @BindView(R.id.rb_fold)
    RadioButton rbFold;


    private CurtainContract.CurtainPresenter presenter;
    private Curtain mCurtain;
    private int level = 0;
    private int oldLevel = 0;
    private int speed = 1;
    private int mPosition;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_curtain;
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCurtainView.setProgress(msg.what);
        }
    };

    @Override
    protected void init() {
        Bundle bundle = getIntent().getExtras();

        mCurtain = (Curtain) bundle.get("curtain");
        mPosition = bundle.getInt("pos");
        level = mCurtain.getStatus();

        presenter = new CurtainPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        mSeekBarStatus.setProgress(level);
        mCurtainView.setProgress(level);
        mCurtainView.setCurtainColor(getResources().getColor(R.color.colorPrimary));
        rbUnfold.setOnClickListener(this);
        rbStop.setOnClickListener(this);
        rbFold.setOnClickListener(this);

        mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                speed = progress + 1;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mSeekBarStatus.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
                handler.sendEmptyMessage(progress);
                oldLevel = level;
                level = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(level > oldLevel){
                    presenter.unfold(mCurtain.getId(), level, speed);
                }
                if(level < oldLevel){
                    presenter.fold(mCurtain.getId(), level, speed);
                }

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rb_unfold:
                presenter.unfold(mCurtain.getId(), level, speed);
                break;
            case R.id.rb_stop:
                presenter.stop(mCurtain.getId());
                break;
            case R.id.rb_fold:
                presenter.fold(mCurtain.getId(), level, speed);
                break;
        }
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(mCurtain.getDescription());
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    public void unfoldSuccess() {

    }

    @Override
    public void foldSuccess() {

    }

    @Override
    public void stopSuccess() {

    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //发送照明设备状态变化广播
        Intent intent = new Intent(C.Action.CURTAIN_STATUS_CHANGED);
        Bundle bundle = new Bundle();
        bundle.putInt("level", level);
        bundle.putInt("pos", mPosition);
        intent.putExtras(bundle);
        sendBroadcast(intent);
        presenter.dettachView();

    }
}
